<!-- 表单弹窗 -->
<form id="paper-form" lay-filter="paper-form" class="layui-form model-form">
    <input name="papeid" type="hidden"/>
    <!--发布人-->
    <input name="publisher" id="publisher" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-badge-dot"></span>问卷标题</label>
        <div class="layui-input-block">
            <input name="title" placeholder="请输入问卷标题" type="text" class="layui-input"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-badge-dot"></span>面对群体</label>
        <div class="layui-input-block">
            <select name="faceGroup" id="faceGroup" lay-filter="selected-faceGroup" lay-verify="required" required>
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" id="respondents-div" style="display: none">
        <label class="layui-form-label"><span class="layui-badge-dot"></span>调查对象</label>
        <div class="layui-input-block">
            <select name="respondents" id="respondents" lay-verify="required" required>
                <option value="" id="respondents-name">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-badge-dot"></span>评分类型</label>
        <div class="layui-input-block">
            <select name="rateTypeCode" id="rateTypeCode" lay-verify="required" required>
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-badge-dot"></span>结束日期</label>
        <div class="layui-input-block">
            <input name="endTime" id="endTime" placeholder="yyyy-MM-dd" type="text" class="layui-input"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input name="sort" placeholder="请输入排序号" type="number" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问卷说明</label>
        <div class="layui-input-block">
            <textarea name="introduce" placeholder="请输入问卷说明" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="paper-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'laydate', 'config'], function () {
        let layer = layui.layer;
        let admin = layui.admin;
        let form = layui.form;
        let laydate = layui.laydate;
        let config = layui.config;

        //开启公历节日
        laydate.render({
            elem: '#endtim'
            , calendar: true
            , trigger: 'click'
        });
        //获取当前登录用户
        let loginUser = config.getUser();
        $("#publisher").val(loginUser.username);

        let paper = admin.getTempData('t_paper');
        if (paper){
            $('#respondents-div').attr('style', '{display:block}');
            $('#respondents-name').html(paper.respdt);
        }

        // 获取角色列表
        layer.load(2);
        admin.req('api-user/roles', {}, function (data) {
            layer.closeAll('loading');
            if (0 === data.code) {
                //往下拉菜单里添加元素
                $.each(data.data, function (index, item) {
                    $('#faceGroup').append(new Option(item.name, item.id));
                });
                form.render();
            } else {
                layer.msg('获取角色列表', {icon: 2, time: 500});
            }
        }, 'GET');

        // 获取评分类型列表
        admin.req('api-paper/rateType/getRateTypeList', {}, function (data) {
            layer.closeAll('loading');
            if (0 === data.code) {
                //往下拉菜单里添加元素
                $.each(data.data, function (index, item) {
                    $('#rateTypeCode').append(new Option(item.rateName, item.rateCode));
                });
                form.render();
                // 回显数据
                $('#paper-form').attr('method', 'POST');
                if (paper) {
                    form.val('paper-form', paper);
                }
            } else {
                layer.msg('获取类型列表', {icon: 2, time: 500});
            }
        }, 'GET');

        //select事件
        form.on('select(selected-faceGroup)', function () {
            let roleId = $('#faceGroup').val();
            if (roleId) {
                $('#respondents-div').attr('style', '{display:block}');
                getRespondent(roleId);
            }
        });

        //获取调查对象
        function getRespondent(roleId){
            layer.load(2);
            admin.req('api-user/users/findByRoleId/'+roleId, {}, function (data) {
                layer.closeAll('loading');
                if (0 === data.code) {
                    //往下拉菜单里添加元素
                    $.each(data.data, function (index, item) {
                        $('#respondents').append(new Option(item.username, item.username));
                    });
                    form.render();
                } else {
                    layer.msg('获取调查对象列表', {icon: 2, time: 500});
                }
            }, 'GET');
        }


        // 表单提交事件
        form.on('submit(paper-form-submit)', function (data) {
            layer.load(2);
            admin.req('api-paper/paper/saveOrUpdate', JSON.stringify(data.field), function (data) {
                layer.closeAll('loading');
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, $('#paper-form').attr('method'));
            return false;
        });
    });


</script>
